package cn.swufe.edu.stu.hw_tools.Time;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import cn.swufe.edu.stu.hw_tools.R;

public class DailyActivity extends AppCompatActivity {
    private int taskNum = 0;
    private String task[] = new String[10];
    private Button backbtn,insertbtn,resetbtn;
    private ArrayAdapter listItemAdapter;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_daily);

        //返回按钮监听器
        backbtn = findViewById(R.id.dailyback);
        backbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = getIntent();
                setResult(2,intent);
                finish();
            }
        });

        //添加任务按钮监听器
        insertbtn = findViewById(R.id.dailyinsert);
        insertbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView task = findViewById(R.id.dailyeditText);
                String atask = task.getText().toString();
                if(atask.equals("")){
                    Toast.makeText(DailyActivity.this, "。。。这是聪明的app才能看见的任务吗(ノ゜▽゜)？", Toast.LENGTH_SHORT).show();
                }else if(taskNum == 10){
                    Toast.makeText(DailyActivity.this, "。。。任务太多了(ノ゜▽゜)？", Toast.LENGTH_SHORT).show();
                } else{
                    SharedPreferences sp2 = getSharedPreferences("daily", Activity.MODE_PRIVATE);
                    SharedPreferences.Editor editor2 = sp2.edit();
                    editor2.putString("task"+ (taskNum+1),atask);
                    editor2.apply();
                    adjustListView();
                    task.setText("");
                    Toast.makeText(DailyActivity.this, "添加成功,记得按时完成໒( ̿. ᴥ ̿. )ʋ", Toast.LENGTH_SHORT).show();
                }
            }
        });

        resetbtn = findViewById(R.id.dailyReset);
        resetbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(DailyActivity.this, "重置成功", Toast.LENGTH_SHORT).show();
                initListView();
            }
        });

        //初始化今日任务为空
        listView = findViewById(R.id.dailylist);
        adjustListView();

        //listview监听器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(DailyActivity.this);
                builder.setTitle("任务完成");
                builder.setMessage("确定这个任务已经完成了吗？");
                builder.setPositiveButton("搞定了", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // 确保listItemAdapter与之前创建的适配器实例相同
                        //listItemAdapter.remove(listitems.get(position)); // 使用listItems的get方法获取项
                        //listItemAdapter.notifyDataSetChanged();
                        SharedPreferences sp2 = getSharedPreferences("daily", Activity.MODE_PRIVATE);
                        SharedPreferences.Editor editor2 = sp2.edit();
                        editor2.putString("task"+(position+1),"暂无");
                        editor2.apply();
                        adjustListView();
                        TextView task = findViewById(R.id.dailyeditText);
                    }
                });
                builder.setNegativeButton("点错了", null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
    }

    public void initListView(){
        //添加初始任务
        SharedPreferences sp = getSharedPreferences("daily", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        for (int i = 1 ;i <=10 ; i ++ ) {
            editor.putString("task"+i,"暂无");
        }
        editor.apply();
        //获取列表并适配
        listView = findViewById(R.id.dailylist);
        ArrayList<String> listitems = new ArrayList<String>();
        for(int i=0;i<10;i++){
            task[i] =sp.getString("task"+(i+1) ,"暂无");
            if(!task[i].equals("暂无")){
                listitems.add(task[i]);
            };
        }
        // 生成适配器的Item和动态数组对应的元素
        listItemAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, listitems);
        listView.setAdapter(listItemAdapter);
    }

    public void adjustListView(){
        ArrayList<String> listitems = new ArrayList<String>();
        SharedPreferences sp3 = getSharedPreferences("daily", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor3 = sp3.edit();
        taskNum = 0;
        for(int i=0;i<10;i++){
            task[i] =sp3.getString("task"+(i+1) ,"暂无");
            if(!task[i].equals("暂无")){
                taskNum++;
                listitems.add(task[i]);
            };
        }
        int j = taskNum;
        for(int i=0;i<10;i++){
            if(j>0){editor3.putString("task"+(i+1),listitems.get(i));j--;}
            else {editor3.putString("task"+(i+1),"暂无");}
        }
        editor3.apply();
        listItemAdapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, listitems);
        listView.setAdapter(listItemAdapter);
        if(taskNum == 0){
            Toast.makeText(this, "          ╲(｡◕‿◕｡)╱太棒了！\n╲(｡◕‿◕｡)╱今天的任务完成啦~~~", Toast.LENGTH_SHORT).show();
        }
    }

}